package fr.emse.tatiana.scoresheetvisualisation;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.SelectionEditPolicy;
import org.eclipse.swt.graphics.Color;

import java.util.List;
import java.util.Vector;

import fr.emse.tatiana.graphical.DisplayRule;
import fr.emse.tatiana.replayable.IReplayableModel;
import fr.emse.tatiana.replayable.analysis.IAnalysis;
import fr.emse.tatiana.replayable.analysis.IGraphAnalysis;
import fr.emse.tatiana.replayable.analysis.Link;

public class EventPart extends AbstractGraphicalEditPart {

	private List<DisplayRule> rules;
	private IReplayableModel replayablemodel;
	private String tooltip;
	private ReplayableModelPart parent;


	
	public EventPart(Object model, List<DisplayRule> rules) {
		super();
		setModel(model);
		this.rules = rules;
	}

	// copied from GraphAnalysis
	public String chooseTextForEvent(IReplayableModel m, Object event) {
		String ret = "";
		for (int i=0; i<m.getFacetCount() && !m.isAnalysisFacet(i); i++) {
			String ftext = m.getFacetText(event, i);
			if (ftext!=null && ftext.length() > ret.length()) {
				ret = ftext;
			}
		}
		return ret;
	}
	
	@Override
	protected IFigure createFigure() {
		EventFigure f = new EventFigure();
		return new EventFigure();
	}

    @Override
	protected void refreshVisuals(){
        EventFigure figure = (EventFigure)getFigure();
		this.parent = ((ReplayableModelPart)getParent());
        this.replayablemodel = (IReplayableModel) parent.getModel();
		this.tooltip = chooseTextForEvent(replayablemodel, getModel());
        
        long first = replayablemodel.getEventTimestamps().get(0);
        long fromfirst = replayablemodel.getBeginTime(getModel())-first;
        long duration = replayablemodel.getLastTime(getModel())-replayablemodel.getBeginTime(getModel());
        Rectangle r = new Rectangle((parent.millisToCoord(fromfirst))+20,50, parent.millisToCoord(duration+10)+1,10);
        figure.setAbsoluteX(r.x);
        figure.setAbsoluteY(r.y);
        figure.setWidth(r.width);
        figure.setHeight(r.height);
        figure.setBackgroundColor(ColorConstants.white);
        figure.setBorder(new LineBorder());
        figure.setVisible(true);
        figure.setShape(null);
        figure.setToolTip(new Label(tooltip));

        for (DisplayRule rule: rules) {
        	rule.apply(replayablemodel, getModel(), figure);
        }
        figure.repaint();
        ((ReplayableModelPart)getParent()).expandToContain(figure.getBounds());
        
    }
    

	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new SelectionEditPolicy() {

			IFigure feedbackfigure;
			
			@Override
			protected void hideSelection() {
				if (feedbackfigure != null) {
					removeFeedback(feedbackfigure);
				}
				feedbackfigure = null;
			}

			@Override
			protected void showSelection() {
				if (feedbackfigure!=null) {
					return;
				}
				feedbackfigure = new Figure();
				IFigure figure = getHostFigure();
				Rectangle r = figure.getBounds().getExpanded(5, 5);
				feedbackfigure.setBounds(r);
				feedbackfigure.setBorder(new LineBorder(new Color(null, 255, 0, 100),4));
				addFeedback(feedbackfigure);
			}
			
		});

	}
	
	protected ConnectionAnchor getConnectionAnchor() {

		return new ChopboxAnchor(getFigure());

	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelSourceConnections()
	 */
	@Override
	@SuppressWarnings("unchecked")
	protected List getModelSourceConnections() {
		ReplayableModelPart parent = ((ReplayableModelPart)getParent());
        IReplayableModel model = (IReplayableModel) parent.getModel();
		List<Link> l = new Vector<Link>();
		for (IAnalysis a : model.getAnalyses()) {
			if (a instanceof IGraphAnalysis) {
				l.addAll(((IGraphAnalysis) a).getSourceConnections(model.getAnchor(getModel())));
			}
		}
		return l;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelTargetConnections()
	 */
	@Override
	protected List getModelTargetConnections() {

		ReplayableModelPart parent = ((ReplayableModelPart)getParent());
        IReplayableModel model = (IReplayableModel) parent.getModel();
		List<Link> l = new Vector<Link>();
		for (IAnalysis a : model.getAnalyses()) {
			if (a instanceof IGraphAnalysis) {
				l.addAll(((IGraphAnalysis) a).getTargetConnections(model.getAnchor(getModel())));
			}
		}
		return l;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.Request)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}


}
